#set( $symbol_pound = '#' )
#set( $symbol_dollar = '$' )
#set( $symbol_escape = '\' )
package ${package}.demo.controller;

import ${package}.demo.model.Product;
import ${package}.demo.service.ProductService;
import com.ucarinc.framework.common.ConResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author liuxw
 * @date 2019/3/28
 * @since 1.0
 */
@RestController
@RequestMapping("/product")
public class ProductController {

    @Autowired
    private ProductService productService;

    @RequestMapping("/getPage")
    public ConResult getUserList(String productName,
                                 @RequestParam(value = "pageNo",defaultValue = "1") int pageNo,
                                 @RequestParam(value = "pageSize",defaultValue = "10") int pageSize){

        int startPage = (pageNo -1) * pageSize;

        Map<String,Object> param = new HashMap<>();
        param.put("pageSize",pageSize);
        param.put("startPage",startPage);
        param.put("productName",productName);

        List list = productService.getPageList(param);
        Integer total = productService.getPageListCount(param);

        //PageInfo pageModel = new PageInfo(list,total,pageNo);

        Map<String,Object> result = new HashMap<>();
        result.put("list",list);
        result.put("pageNo",pageNo);
        result.put("pageSize",pageSize);
        result.put("total",total);

        return ConResult.success(result);
    }

    @RequestMapping("/save")
    public ConResult save(HttpServletRequest request){

        Product product = new Product();
        product.setProductName(request.getParameter("productName"));
        product.setCount(Integer.valueOf(request.getParameter("count")));
        product.setCreateTime(new Date());
        product.setUpdateTime(new Date());

        Integer id = productService.save(product);
        if(id > 0){
            return ConResult.success("添加成功");
        }else {
            return ConResult.fail("添加用户失败");
        }
    }

    @RequestMapping("/delete")
    public ConResult delete(HttpServletRequest request){
        String id = request.getParameter("id");
        if(null == id || "".equals(id)){
            return ConResult.fail("删除失败，请检查参数");
        }

        Integer result = productService.deleteById(Integer.valueOf(id));
        if(result > 0){
            return ConResult.success("删除成功");
        }else {
            return ConResult.fail("删除失败");
        }
    }

}
